大家好,我是毛毛。ヾ(´∀ ˋ)ノ
那就開始今天的解題吧~
Given two strings s
and t
, return true if t
is an anagram of s, and false
otherwise.
An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
Example 1:
Input: s = "anagram", t = "nagaram"
Output: true
Example 2:
Input: s = "rat", t = "car"
Output: false
Constraints:
1 <= s.length, t.length <= 5 * 10^4
s
and t
consist of lowercase English letters.Follow up: What if the inputs contain Unicode characters? How would you adapt your solution to such a case?
判斷s是否可以重新組合出t這個字。
想法是把s跟t分別拆解,用dictionary存每個字母出現次數,如果兩者一樣,就代表OK~
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
dict_s = {}
dict_t = {}
for index in range(len(s)):
if s[index] in dict_s:
dict_s[s[index]] += 1
else:
dict_s[s[index]] = 1
for index in range(len(t)):
if t[index] in dict_t:
dict_t[t[index]] += 1
else:
dict_t[t[index]] = 1
return dict_s == dict_t
今天就到這邊啦~
大家明天見